<template>
  <el-dialog :title="'[' + tableName + ']数据项规则列表'" :visible.sync="visible" :append-to-body="appendToBody" :fullscreen="true" :before-close="onClose">
    <el-card>
      <el-tabs v-model="activeName">
        <el-tab-pane label="基本信息" name="basic">
          <basic-info-form ref="basicInfo" :info="info" />
        </el-tab-pane>
        <el-tab-pane label="字段信息" name="column">
          <el-table
            ref="dragTable"
            v-loading="tableLoading"
            :data="tableData"
            element-loading-text="加载中"
            row-key="columnName"
            align="left"
            :max-height="tableHeight"
          >
            <el-table-column label="序号" type="index" min-width="15px" class-name="allowDrag" />
            <el-table-column
              label="字段列名"
              prop="columnName"
              min-width="150px"
              :show-overflow-tooltip="true"
              class-name="allowDrag"
            />
            <el-table-column label="字段描述" min-width="220px">
              <template slot-scope="scope">
                <el-input v-model="scope.row.columnComment" />
              </template>
            </el-table-column>
            <el-table-column
              label="物理类型"
              prop="columnType"
              min-width="180px"
              :show-overflow-tooltip="true"
              class-name="allowDrag"
            />
            <el-table-column label="Java类型" min-width="180px">
              <template slot-scope="scope">
                <el-select v-model="scope.row.javaType">
                  <el-option label="String" value="String" />
                  <el-option label="Integer" value="Integer" />
                  <el-option label="Long" value="Long" />
                  <el-option label="Double" value="Double" />
                  <el-option label="BigDecimal" value="BigDecimal" />
                  <el-option label="Date" value="Date" />
                </el-select>
              </template>
            </el-table-column>
            <el-table-column label="Java字段名" min-width="180px">
              <template slot-scope="scope">
                <el-input v-model="scope.row.javaField" />
              </template>
            </el-table-column>
            <el-table-column label="插入" min-width="35px">
              <template slot-scope="scope">
                <el-checkbox v-model="scope.row.isInsert" true-label="0" />
              </template>
            </el-table-column>
            <el-table-column label="编辑" min-width="35px">
              <template slot-scope="scope">
                <el-checkbox v-model="scope.row.isEdit" true-label="0" />
              </template>
            </el-table-column>
            <el-table-column label="列表" min-width="35px">
              <template slot-scope="scope">
                <el-checkbox v-model="scope.row.isList" true-label="0" />
              </template>
            </el-table-column>
            <el-table-column label="查询" min-width="35px">
              <template slot-scope="scope">
                <el-checkbox v-model="scope.row.isQuery" true-label="0" />
              </template>
            </el-table-column>
            <el-table-column label="查询方式" min-width="180px">
              <template slot-scope="scope">
                <el-select v-model="scope.row.queryType">
                  <el-option label="EQ" value="EQ" />
                  <el-option label="NEQ" value="NEQ" />
                  <el-option label="GT" value="GT" />
                  <el-option label="LT" value="LT" />
                  <el-option label="GE" value="GE" />
                  <el-option label="LE" value="LE" />
                  <el-option label="LK" value="LK" />
                  <el-option label="IN" value="IN" />
                  <el-option label="NIN" value="NIN" />
                  <el-option label="EMP" value="EMP" />
                  <el-option label="NEMP" value="NEMP" />
                  <el-option label="NULL" value="NULL" />
                  <el-option label="NNULL" value="NNULL" />
                  <el-option label="BETWEEN" value="BETWEEN" />
                </el-select>
              </template>
            </el-table-column>
            <el-table-column label="必填" min-width="35px">
              <template slot-scope="scope">
                <el-checkbox v-model="scope.row.isRequired" true-label="0" />
              </template>
            </el-table-column>
            <el-table-column label="显示类型" min-width="180px">
              <template slot-scope="scope">
                <el-select v-model="scope.row.htmlType">
                  <el-option label="文本框" value="input" />
                  <el-option label="文本域" value="textarea" />
                  <el-option label="下拉框" value="select" />
                  <el-option label="单选框" value="radio" />
                  <el-option label="复选框" value="checkbox" />
                  <el-option label="日期控件" value="datetime" />
                </el-select>
              </template>
            </el-table-column>
            <el-table-column label="字典类型" min-width="180px">
              <template slot-scope="scope">
                <el-select v-model="scope.row.dictType" clearable filterable placeholder="请选择">
                  <el-option
                    v-for="dict in optionOfType"
                    :key="dict.dictGroup"
                    :label="dict.dictGroup"
                    :value="dict.dictGroup"
                  >
                    <span style="float: left">{{ dict.dictGroup }}</span>
                  </el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column label="验证规则" min-width="80px">
              <template slot-scope="scope">
                <el-link @click="handleRuleForm(scope.row.columnId)">配置<i class="el-icon-view el-icon--right" /></el-link>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="生成信息" name="genInfo">
          <gen-info-form ref="genInfo" :info="info" />
        </el-tab-pane>
      </el-tabs>
    </el-card>

    <gen-column-rule v-if="openRuleDialog" ref="ruleDialog" />
    <span slot="footer" class="dialog-footer line">
      <el-button v-if="hasLoaded" type="primary" icon="el-icon-check" @click="handleSave">确 定</el-button>
      <el-button icon="el-icon-close" @click="onClose">关 闭</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { loadTable, loadColumn, generatorColumnSave } from '@/api/tool/generator'
import { dictGroup } from '@/api/system/dict'
import BasicInfoForm from './basicInfoForm'
import GenInfoForm from './genInfoForm'
import GenColumnRule from './genColumnRule'
import Sortable from 'sortablejs'

export default {
  name: 'GenColumnEdit',
  components: {
    BasicInfoForm,
    GenInfoForm,
    GenColumnRule
  },
  data() {
    return {
      visible: false,
      appendToBody: false,
      hasLoaded: false,
      tableId: undefined,
      tableName: '',
      // 选中选项卡的 name
      activeName: 'basic',
      // 表格的高度
      tableHeight: document.documentElement.scrollHeight - 280 + 'px',
      tableLoading: false,
      // 表列信息
      tableData: [],
      // 字典信息
      optionOfType: [],
      // 表详细信息
      info: {},
      openRuleDialog: false
    }
  },
  methods: {
    init(tableId, tableName) {
      this.visible = true
      this.hasLoaded = false
      this.tableId = tableId
      this.getColumnData()
      this.$nextTick(() => {
        this.info = {}
        this.tableData = []
        this.tableName = tableName
        this.activeName = 'basic'
        this.getTableData()
        this.getDictData()
      })
    },
    getTableData() {
      loadTable(this.tableId).then(response => {
        this.info = response.param
        // 判断当前用户权限
        this.hasLoaded = this.$perm.hasPermissionOr(['tool:gen:edit'])
      }).catch(() => {})
    },
    getDictData() {
      dictGroup({ onlyGroup: true }).then(response => {
        this.optionOfType = response
      }).catch(e => {})
    },
    getColumnData() {
      this.tableLoading = true
      loadColumn(this.tableId).then(response => {
        this.tableData = response.param.data
        this.tableLoading = false
        this.tableRowDrop()
      }).catch(e => {
        this.tableLoading = false
      })
    },
    tableRowDrop() {
      const tbody = document.querySelector('.el-table__body-wrapper > table > tbody')
      const _this = this
      Sortable.create(tbody, {
        onEnd({ newIndex, oldIndex }) {
          const currRow = _this.tableData.splice(oldIndex, 1)[0]
          _this.tableData.splice(newIndex, 0, currRow)
          for (const index in _this.tableData) {
            _this.tableData[index].sort = parseInt(index) + 1
          }
        }
      })
    },
    handleRuleForm(columnId) {
      this.openRuleDialog = true
      this.$nextTick(() => {
        this.$refs.ruleDialog.init(columnId)
      })
    },
    /** 提交按钮 */
    handleSave() {
      const basicForm = this.$refs.basicInfo.$refs.basicInfoForm
      const genForm = this.$refs.genInfo.$refs.genInfoForm
      const genTable = Object.assign({}, basicForm.model, genForm.model)
      genTable.columns = this.tableData
      generatorColumnSave(this.tableId, genTable).then(response => {
        this.$modal.msgSuccess(response.msg)
        this.onClose()
      }).catch(() => {})
    },
    /** 关闭按钮 */
    onClose() {
      this.openRuleDialog = false
      this.info = {}
      this.tableId = undefined
      this.tableName = ''
      this.tableData = []
      this.activeName = 'column'
      this.visible = false
    }
  }
}
</script>
